﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class EntityObjectAttributeELBDao : DaoService<EntityObjectAttribute>
    {
        public override string TableName => "FAS_EntityObjectAttribute";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "EntityObjectID";

        public override List<string> ExceptColumnName {get;} = DaoUtil.GetExceptionColumn<EntityObjectAttribute>();

        public EntityObjectAttributeELBDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<EntityObjectAttribute> GetEntityObjectAttributes(string eoId)
        {
            var result = new List<EntityObjectAttribute>();
            if (string.IsNullOrEmpty(eoId))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{eoId}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as EntityObjectAttribute));
            }
            return result;
        }

        /// <summary>
        /// 添加EntityObjectAttribute
        /// </summary>
        /// <param name="eoAttributes"></param>
        public void CreateEntityObjectAttribute(List<EntityObjectAttribute> eoAttributes)
        {
            try
            {
                //Attribute中Precision是MySQL中关键词，所以采用自己生成的SQL
                DaoUtil.ExecuteInsertSQL(eoAttributes, this);    //创建EO.Attr
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.EO.CreateAttrError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除EntityObjectAttribute
        /// </summary>
        /// <param name="EntityObjectAttributes"></param>
        public void DeleteEntityObjectAttribute(string eoId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, eoId));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.EO.DeleteAttrError, e.Message, e);
            }
        }

    }
}
